import PySimpleGUI as sg

# 定义计算差额投资回收期的函数
def calculate_difference_payback_period(initial_investment_1, operating_cost_1, initial_investment_2,
                                        operating_cost_2):
   if initial_investment_1 > initial_investment_2:
        difference_investment = initial_investment_1 - initial_investment_2
        difference_operating_cost = operating_cost_2 - operating_cost_1
   else:
       difference_investment = initial_investment_2 - initial_investment_1
       difference_operating_cost = operating_cost_1 - operating_cost_2

   payback_period = difference_investment / difference_operating_cost

   if payback_period <= 0:
       return "结果非正，无法计算差额投资回收期。"
   else:
       return payback_period


# 设置窗口布局
layout = [
    [sg.Text("方案一 期初投资："), sg.InputText(key="investment_1",default_text= "1000")],
    [sg.Text("方案一 经营成本："), sg.InputText(key="operating_cost_1",default_text= "500")],
    [sg.Text("方案二 期初投资："), sg.InputText(key="investment_2",default_text= "2000")],
    [sg.Text("方案二 经营成本："), sg.InputText(key="operating_cost_2",default_text= "400")],
    [sg.Button("计算差额投资回收期")],
    [sg.Text("", key="result")]
]

# 创建窗口
window = sg.Window("差额投资回收期计算", layout)

while True:
    event, values = window.read()
    if event == sg.WINDOW_CLOSED or event == "退出":
        break

    elif event == "计算差额投资回收期":
        try:
            investment_1 = float(values["investment_1"])
            operating_cost_1 = float(values["operating_cost_1"])
            investment_2 = float(values["investment_2"])
            operating_cost_2 = float(values["operating_cost_2"])

            result = calculate_difference_payback_period(investment_1, operating_cost_1, investment_2, operating_cost_2)
            window["result"].update(f"差额投资回收期为：{result}")
        except ValueError:
            window["result"].update("请输入正确的数字！")

window.close()